/**
  ******************************************************************************
  * @file    Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c 
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and 
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
//#include "comm.h"           // For USART or SPI
#include "stk_p.h"          // For SYSTEM_RESET (watchdog), RTC
#include "stm32f10x_plm.h"  // For power line modem
#include <stdio.h>

/** @addtogroup STM32F10x_StdPeriph_Template
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

uint8_t bIndex=0,pbuffer[100],maxFrame=0;
uint8_t *buffer;
typedef enum 
{
  USART_FLAG_FALSE = 0, 
  USART_FLAG_TRUE = (!(USART_FLAG_FALSE))
}usart_flag_t;
typedef struct
{
  uint8_t     	USART_F_len;
  uint8_t     	USART_F_Xmode;
  usart_flag_t  USART_F_TXOK;
  usart_flag_t  USART_F_RXOK;
  usart_flag_t  USART_F_CommNotAllowed;
}USART_NTWFlag_t;
USART_NTWFlag_t uFlag;
#define USART_MODE_HEADER 1
#define USART_MODE_DATA		2
uint8_t USART_MODE = USART_MODE_HEADER;
/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/
uint8_t uFrame_Arrived(void)
	{
		return uFlag.USART_F_RXOK;
	}
void clear_RXOK(void)
	{
		uFlag.USART_F_RXOK = USART_FLAG_FALSE;
	}	
/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
    /* Watchdog start flag */
    SYSTEM_RESET = TRUE;
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
    /* Watchdog start flag */
    SYSTEM_RESET = TRUE;
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
    /* Watchdog start flag */
    SYSTEM_RESET = TRUE;
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
    /* Watchdog start flag */
    SYSTEM_RESET = TRUE;
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
}


/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/


/*******************************************************************************
* Function Name  : EXTI9_5_IRQHandler
* Description    : This function handles External interrupt Line 9 to 5 request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void EXTI9_5_IRQHandler(void)
{
}

/*******************************************************************************
* Function Name  : EXTI15_10_IRQHandler
* Description    : This function handles External interrupt Line 15 to 10 request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void EXTI15_10_IRQHandler(void)
{
}
/*******************************************************************************
* Function Name  : USART1_IRQHandler
* Description    : This function handles USART1 interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USART1_IRQHandler(void)
{
	if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
		{
			if(USART_MODE==USART_MODE_HEADER)
				{
					printf("receive 1st byte\r\n");
					uFlag.USART_F_len = USART_ReceiveData(USART1)-1;
					USART_MODE = USART_MODE_DATA;
					uFlag.USART_F_RXOK = USART_FLAG_FALSE;
					return;
				}
				
			if(USART_MODE == USART_MODE_DATA)
				{
					pbuffer[bIndex] = USART_ReceiveData(USART1);
					printf("%d %d \r\n",pbuffer[bIndex],bIndex);
					bIndex++;
					if(bIndex>uFlag.USART_F_len)
						{
							bIndex=0;
							USART_MODE = USART_MODE_HEADER;
							uFlag.USART_F_RXOK = USART_FLAG_TRUE;
						}	
				}
			USART_ClearITPendingBit(USART1,USART_IT_RXNE);		
		}
	
}

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
